package com.ysd.lis.service.lab.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.lab.LabBarCodeType;
import com.ysd.lis.entity.lab.LabSampleType;
import com.ysd.lis.entity.lab.LabTatBaseruleDetail;
import com.ysd.lis.entity.lab.LabTatNode;
import com.ysd.lis.mapper.lab.LabTatBaseruleDetailMapper;
import com.ysd.lis.service.SysBasDictDetailService;
import com.ysd.lis.service.lab.LabTatBaseruleDetailService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * TAT监控基础规则明细 服务实现类
 * </p>
 *
 * @author cfz
 * @since 2024-10-25
 */
@Service
public class LabTatBaseruleDetailServiceImpl extends ServiceImpl<LabTatBaseruleDetailMapper, LabTatBaseruleDetail> implements LabTatBaseruleDetailService {
    @Autowired
    LabTatBaseruleDetailMapper labTatBaseruleDetailMapper;

    @Autowired
    LabTatBaseruleDetailService labTatBaseruleDetailService;

    @Autowired
    SysBasDictDetailService sysBasDictDetailService;

    @Override
    public Result findLabTatBaseruleDetailList(LabTatBaseruleDetail labTatBaseruleDetail) {
        List<LabTatBaseruleDetail> list = findLabTatBaseruleDetailArray(labTatBaseruleDetail);
        return Result.succ(1, "请求成功", list);
    }

    @Override
    public List<LabTatBaseruleDetail> findLabTatBaseruleDetailArray(LabTatBaseruleDetail labTatBaseruleDetail) {
        MPJLambdaWrapper<LabTatBaseruleDetail> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(LabTatBaseruleDetail.class);
        queryWrapper.selectAs("tatNode", LabTatNode::getNodeStartNa, LabTatBaseruleDetail::getNodeStartNa);
        queryWrapper.selectAs("tatNode", LabTatNode::getNodeEndNa, LabTatBaseruleDetail::getNodeEndNa);
        queryWrapper.selectAs("smptyp", LabSampleType::getSampleTypeName, LabTatBaseruleDetail::getSampleTypeNa);
        queryWrapper.selectAs("barcode", LabBarCodeType::getBarcodeNa, LabTatBaseruleDetail::getBarcodeTypeNa);
        queryWrapper.eq(LabTatBaseruleDetail::getDelFlag, 0);
        if (StringUtils.isNotBlank(labTatBaseruleDetail.getTatNodeId())) {
            queryWrapper.eq(LabTatBaseruleDetail::getTatNodeId, labTatBaseruleDetail.getTatNodeId());
        }
        if (StringUtils.isNotBlank(labTatBaseruleDetail.getTatNodeNo())) {
            queryWrapper.eq(LabTatBaseruleDetail::getTatNodeNo, labTatBaseruleDetail.getTatNodeNo());
        }
        if (StringUtils.isNotBlank(labTatBaseruleDetail.getTatBaseruleId())) {
            queryWrapper.eq(LabTatBaseruleDetail::getTatBaseruleId, labTatBaseruleDetail.getTatBaseruleId());
        }
        if (StringUtils.isNotBlank(labTatBaseruleDetail.getTatBaseruleNo())) {
            queryWrapper.eq(LabTatBaseruleDetail::getTatBaseruleNo, labTatBaseruleDetail.getTatBaseruleNo());
        }
        queryWrapper.eq(StringUtils.isNotBlank(labTatBaseruleDetail.getId()), LabTatBaseruleDetail::getId, labTatBaseruleDetail.getId());
        queryWrapper.leftJoin(LabTatNode.class, "tatNode", p -> p.eq(LabTatNode::getTatNodeNo, LabTatBaseruleDetail::getTatNodeNo).eq(LabTatNode::getDelFlag, 0).ne(LabTatNode::getTatNodeNo, "").isNotNull(LabTatNode::getTatNodeNo));
        queryWrapper.leftJoin(LabSampleType.class, "smptyp", p -> p.eq(LabSampleType::getSampleTypeCode, LabTatBaseruleDetail::getSampleType).eq(LabSampleType::getDelFlag, 0).ne(LabSampleType::getSampleTypeCode, "").isNotNull(LabSampleType::getSampleTypeCode));
        queryWrapper.leftJoin(LabBarCodeType.class, "barcode", p -> p.eq(LabBarCodeType::getBarcodeNo, LabTatBaseruleDetail::getBarcodeType).eq(LabBarCodeType::getDelFlag, 0).ne(LabBarCodeType::getBarcodeNo, "").isNotNull(LabBarCodeType::getBarcodeNo));
        List<LabTatBaseruleDetail> list = labTatBaseruleDetailMapper.selectList(queryWrapper);
        List<Map<String, Object>> patTypeList = (List<Map<String, Object>>) sysBasDictDetailService.getSysBasDictList("PATTYPE").getData();
        for (LabTatBaseruleDetail m : list) {
            if (org.apache.commons.lang3.StringUtils.isNotBlank(m.getPatType()) && CollectionUtils.isNotEmpty(patTypeList)) {
                patTypeList.stream().filter(a -> a.get("code").equals(m.getPatType())).findFirst().ifPresent(stringObjectMap -> m.setPatTypeNa((String) stringObjectMap.get("name")));
            }
        }
        return list;
    }

    @Override
    public Result addLabTatBaseruleDetail(LabTatBaseruleDetail labTatBaseruleDetail) {
        int i = labTatBaseruleDetailMapper.insert(labTatBaseruleDetail);
        if (i > 0) {
            return Result.succ(1, "新增成功", labTatBaseruleDetail);
        }
        return Result.fail("新增失败");
    }

    @Override
    public Result deleteLabTatBaseruleDetail(LabTatBaseruleDetail labTatBaseruleDetail) {
        labTatBaseruleDetail.setDelFlag(1);
        int i = labTatBaseruleDetailMapper.updateById(labTatBaseruleDetail);
        if (i > 0) {
            return Result.succ(1, "删除成功", labTatBaseruleDetail);
        }
        return Result.fail("删除失败");
    }

    @Override
    public Result deleteLabTatBaseruleDetailList(List<LabTatBaseruleDetail> labTatBaseruleDetails) {
        if (CollectionUtils.isEmpty(labTatBaseruleDetails)) {
            return Result.fail("没有要删除的数据");
        }
        List<String> ids = labTatBaseruleDetails.stream().map(LabTatBaseruleDetail::getId).collect(Collectors.toList());
        int i = labTatBaseruleDetailMapper.deleteBatchIds(ids);
        return i > 0 ? Result.succ(1, "删除成功", labTatBaseruleDetails) : Result.fail("删除失败");
    }

    @Override
    public Result updateLabTatBaseruleDetail(LabTatBaseruleDetail labTatBaseruleDetail) {
        int i = labTatBaseruleDetailMapper.updateById(labTatBaseruleDetail);
        if (i > 0) {
            return Result.succ(1, "更新成功", labTatBaseruleDetail);
        }
        return Result.fail("更新失败");
    }

    /*批量添加*/
    @Override
    public Result addLabTatBaseruleDetailList(List<LabTatBaseruleDetail> labTatBaseruleDetails) {
        boolean i = labTatBaseruleDetailService.saveBatch(labTatBaseruleDetails);
        return i ? Result.succ(1, "更新成功", labTatBaseruleDetails) : Result.fail("更新失败");
    }
}
